/*
 * Copyright (C) 2013 Andreas Stuetz <andreas.stuetz@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.efounder.frame.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.core.xml.StubObject;
import com.efounder.mobilemanager.R;
import com.efounder.frame.activity.EFAppAccountActivity;
import com.efounder.frame.activity.EFAppAccountMainActivityInterface;
import com.efounder.frame.baseui.EFFragment;
import com.efounder.frame.utils.EFFrameUtils;
import com.efounder.frame.xmlparse.EFRegistry;

public class EFDefaultFragment extends EFFragment implements View.OnClickListener{

	private String TAG = "EFDefaultFragment";
	private static final String ARG_POSITION = "position";

	private int position;
	private StubObject stubObject;

	public static EFDefaultFragment newInstance(int position, StubObject stubObject) {
		EFDefaultFragment fragment = new EFDefaultFragment();
		Bundle bundle = new Bundle();
		bundle.putInt(ARG_POSITION, position);
		bundle.putSerializable("stubObject",stubObject);
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		Log.i("--",TAG + "_" + this.hashCode() + "------setUserVisibleHint--isVisibleToUser:" + isVisibleToUser + "--viewPager中的fragment显示隐藏时调用");
	}


	@Override
	public void onAttach(Context context) {
		super.onAttach(context);
		StubObject stubObject = (StubObject) getArguments().getSerializable("stubObject");
		TAG +="_" + stubObject.getCaption() + "_" + this.hashCode() + "-" +getArguments().getInt(ARG_POSITION);
		Log.i("--",TAG + "------onAttach：");
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i("--",TAG + "------onCreate:" + savedInstanceState);
		Bundle bundle = getArguments();
		if (bundle != null){
			position = getArguments().getInt(ARG_POSITION);
			stubObject = (StubObject) bundle.getSerializable("stubObject");
		}
		Activity activity = getActivity();
		Fragment parentFragment = getParentFragment();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		Log.i("--",TAG + "------onCreateView");
		View view = inflater.inflate(R.layout.ef_fragment_test,container,false);
		TextView captionTV = (TextView) view.findViewById(R.id.tv_caption);
		captionTV.setText(stubObject.getCaption()+"_"+position);
		view.findViewById(R.id.goto1_1).setOnClickListener(this);
		view.findViewById(R.id.goto2_2).setOnClickListener(this);
		view.findViewById(R.id.button_yyh).setOnClickListener(this);
		view.findViewById(R.id.button_yl).setOnClickListener(this);
		view.findViewById(R.id.button_yyh_activity).setOnClickListener(this);

		return view;
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.goto1_1) {
			Activity activity = getActivity();
			if (activity instanceof EFAppAccountMainActivityInterface){
				EFAppAccountMainActivityInterface mainActivityInterface = (EFAppAccountMainActivityInterface) activity;
				mainActivityInterface.gotoFragment(1,1);
			}
		}else if (id == R.id.goto2_2) {
			Activity activity = getActivity();
			if (activity instanceof EFAppAccountMainActivityInterface){
				EFAppAccountMainActivityInterface mainActivityInterface = (EFAppAccountMainActivityInterface) activity;
				mainActivityInterface.gotoFragment(2,2);
			}
		}else if (id == R.id.button_yyh) {
			Activity activity = getActivity();
			if (activity instanceof EFAppAccountMainActivityInterface){
				EFAppAccountMainActivityInterface mainActivityInterface = (EFAppAccountMainActivityInterface) activity;
				int[] positions = EFRegistry.getFragmentPositions("yingYongHao");//应用号
				mainActivityInterface.gotoFragment(positions[0],positions[1]);
			}
		}else if (id == R.id.button_yl) {
			Activity activity = getActivity();
			if (activity instanceof EFAppAccountMainActivityInterface){
				EFAppAccountMainActivityInterface mainActivityInterface = (EFAppAccountMainActivityInterface) activity;
				int[] positions = EFRegistry.getFragmentPositions("yuLe");//娱乐
				mainActivityInterface.gotoFragment(positions[0],positions[1]);
			}
		}else if (id == R.id.button_yyh_activity) {
			EFFrameUtils.pushActivity(EFAppAccountActivity.class,null);
		}
	}

	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Log.i("--",TAG + "------onActivityCreated");
	}

	@Override
	public void onStart() {
		super.onStart();
		Log.i("--",TAG + "------onStart");
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.i("--",TAG + "------onResume");
	}

	@Override
	public void onPause() {
		super.onPause();
		Log.i("--",TAG + "------onPause");
	}

	@Override
	public void onStop() {
		super.onStop();
		Log.i("--",TAG + "------onStop");
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		Log.i("--",TAG + "------onDestroyView");
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.i("--",TAG + "------onDestroy");
	}

	@Override
	public void onDetach() {
		super.onDetach();
		Log.i("--",TAG + "------onDetach");
	}



	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString("test","测试onSaveInstanceState");
		Log.i("--",TAG + "------onSaveInstanceState--outState:" + outState);
	}

	@Override
	public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
		super.onViewStateRestored(savedInstanceState);
		Log.i("--",TAG + "------onViewStateRestored--savedInstanceState:" + savedInstanceState);
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		Log.i("--",TAG + "------onHiddenChanged--hidden:" + hidden +"--hide，show时调用此生命周期");
	}

}